﻿/*-----------------------------------------------
// File: UltimateState.cs
// Description: 大招阶段，1. 英雄退回原点 2. 镜头切换 4.播放大招动画 5.结算 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Battle.View
{
    public class UltimateState : Fsm.FsmStateBase<BattleRoundState>
    {
        public BattleRoundCtrl battleRoundCtrl {
            get { return this.controller as BattleRoundCtrl; }
        }
        public UltimateState(BattleRoundState state, BattleRoundCtrl ctrl) : base(state, ctrl) { }


        bool isUltiPaintDispeared = false;
        #region Ultimate Info
        /// <summary>
        /// 当前大招的BehaviourPackage
        /// </summary>
        UltimateBehaviourPackage currentPackage {
            get { return BattleViewCtrl.GetInstance().GetUltimatePackage(); }
        }
        /// <summary>
        /// 当前大招的对象
        /// </summary>
        public UltimateSkill currentSkill;
        /// <summary>
        /// 大招Release队列
        /// </summary>
        Queue<BattleSkillRelease> releaseQueue = new Queue<BattleSkillRelease>();
        /// <summary>
        /// 当前Release
        /// </summary>
        BattleSkillRelease currentRelease;

        public void AddOnceAttackCache(OnceAttackPackageBase p) {
            battleRoundCtrl.AddUltimateCache(p);
        }

        internal void initInfo() {
            currentSkill = sourceBC.GetSkill(SkillType.Ultimate) as UltimateSkill;
            releaseQueue = new Queue<BattleSkillRelease>();
            foreach (var release in currentSkill.releases) {
                releaseQueue.Enqueue(release);
            }
            currentRelease = null;
        }
        #endregion

        #region SourceBC FriendBC OtherBC

        public BattleCharacter sourceBC {
            get { return battleRoundCtrl.GetSource(); }
        }
        public List<BattleCharacter> targetBCs {
            get { return battleRoundCtrl.GetTargets(); }
        }
        public List<BattleCharacter> friendBCs {
            get { return battleRoundCtrl.GetFriends(); }
        }
        public List<BattleCharacter> otherBCs {
            get { return battleRoundCtrl.GetOthers(); }
        }

        internal void initBCs() {
            //battleRoundCtrl.InitBCs(currentPackage);

        }
        #endregion

        #region Ultimating

        void UltimateStart()
        {
            // 隐藏主摄像机
            BattleViewCtrl.GetInstance().battleCamera.enabled = false;
            // 显示大招对象
            UltimateLogic ultiLogic = currentSkill.GetUltimateLogic();
            if(ultiLogic != null)
            {
                ultiLogic.gameObject.SetActive(true);
            }
            // 隐藏主场景
            BattleViewCtrl.GetInstance().HideSceneObj();
            // SourceBC UltimateAttack
            var sourceBCState = sourceBC.GetCurrentState() as BCUltimateState;
            sourceBCState.UltimateAttack();
            // FriendBC TargetBC OtherBC Setting
            BCBeUltimatedState.BeUltimateFriendIndex = 0;
            BCBeUltimatedState.BeUltimateEnemyIndex = 0;
            //List<AttackRespect> immediateRespects = new List<AttackRespect>();
            foreach (var friend in friendBCs)
            {
                friend.gameObject.SetActive(true);
                friend.SwitchState(ActorState.Ultimated);
                //immediateRespects.AddRange(BCStateCacheFactory.GetBCLastRepsects(friend));
            }
            foreach (var target in targetBCs)
            {
                target.gameObject.SetActive(true);
                target.SwitchState(ActorState.Ultimated);
                //immediateRespects.AddRange(BCStateCacheFactory.GetBCLastRepsects(target));
            }
            foreach (var other in otherBCs)
            {
                other.ClearParticleRecoveryTask();
                other.gameObject.SetActive(false);
                //immediateRespects.AddRange(BCStateCacheFactory.GetBCLastRepsects(other));
            }

            // 大招立即结算的RESPECT
            //foreach (var respect in immediateRespects) {
            //    ViewRespectCtrl.GetInstance().AddRespect(respect, BattleViewCtrl.GetInstance().UltimateImmediateRespectCallBack);
            //    var bc = BattleViewCtrl.GetInstance().GetBattleCharacter(respect.sourceUid);
            //    var skill = bc.GetSkill(respect.skillType);
            //    skill.IsCasted = true;
            //}
            deltaTime = 0f;
            // 大招减怒气
            BattleViewCtrl.GetInstance().AddAnger(-Dict.Blo.DictConfigBlo.GetUltimateCostAngerNum(), sourceBC.camp);

            BattleManager.Instance.battlePanel.ShowUltimateCoverPanel();
            BattleManager.Instance.battlePanel.ShowUltimateSkipTip();
            RenderSettings.fog = false;
        }

        public const float ultiPaintDispearTime = 0.1f;
        internal void UltimateTick() {
            deltaTime += Time.deltaTime;
            if (deltaTime > ultiPaintDispearTime && !isUltiPaintDispeared) {
                BattleManager.Instance.battlePanel.HideUltimatePaint();
                isUltiPaintDispeared = true;
            }

            if (currentRelease == null && releaseQueue.Count > 0)
                currentRelease = releaseQueue.Dequeue();
            if (currentRelease != null) {
                if (deltaTime >= currentRelease.releaseTime) {
                    OnSkillRelease(currentRelease);
                    currentRelease = null;
                }
            }
            if (deltaTime >= currentSkill.animDuration) {
                UltimateOver();
            }
        }
        void OnSkillRelease(BattleSkillRelease release)
        {
            var state = sourceBC.GetCurrentState() as BCUltimateState;
            if (state != null)
            {
                state.OnSkillRelease(release);
            }
            else {
                Debug.LogError("[UltimateState OnSkillRelease] SourceBC Not In UltimateState! state:" + sourceBC.GetCurrrentStateType().ToString());
            }
        }

        void UltimateOver() {
            BattleViewCtrl.GetInstance().DoneUltimateBehaviour();
            // 状态还原
            sourceBC.SwitchState(ActorState.AttackReady);
            foreach (var stateCache in battleRoundCtrl.GetStateCaches()) {
                stateCache.Revert();
            }
            // 显示主摄像机
            BattleViewCtrl.GetInstance().battleCamera.enabled = true;
            // 显示主场景
            BattleViewCtrl.GetInstance().ShowSceneObj();
            // 隐藏大招对象
            var ul = currentSkill.GetUltimateLogic();
            if (ul != null)
            {
                currentSkill.GetUltimateLogic().gameObject.SetActive(false);
            }
            battleRoundCtrl.SwitchState(BattleRoundState.UltimateEndState);
        }

        #endregion


        bool CheckUltimateResource()
        {
            string aniName = sourceBC.GetSkill(SkillType.Ultimate).animName;
            if (sourceBC.anim.anim.GetClip(aniName) == null)
            {
                return false;
            }
            var logic = BattleViewCtrl.GetInstance().GetUltimateLogic(currentSkill.HSId);
            if (logic == null)
            {
                return false;
            }
            return true;
        }

        public override void Enter()
        {
            initBCs();
            initInfo();
            UltimateStart();
            isUltiPaintDispeared = false;

            CameraController.Instance.ReadyForUltimateEnd(currentSkill.cameraEndType);

            //if (!CheckUltimateResource())
            //{
            //    BattleViewCtrl.GetInstance().AskJumpUltimate();
            //}
        }
        float deltaTime = 0f;
        public override void Update()
        {
            UltimateTick();
        }

        public override void Leave()
        {
            BattleManager.Instance.battlePanel.HideUltimateSkipTip();
            RenderSettings.fog = true;
            Debug.Log("[UltimateState Leave]  RESPECT COUNT:" + ViewRespectCtrl.GetInstance().GetRespectCount());
            Debug.Log("[UltimateState Leave]  RESPONSE COUNT:" + ViewRespectCtrl.GetInstance().GetResponseCount());
        }

        public void Skip() {
            deltaTime = currentSkill.animDuration - 0.1f;
            currentRelease = null;
            releaseQueue.Clear();
            currentSkill.StopSkillAudio();
        }

        public void SkipReleaseCallBack(ResponseBase rb) {
            var attackPackage = rb.packageData as OnceAttackPackage;
            AddOnceAttackCache(rb.packageData);
            attackPackage.UltimateDeal();
        }



    }

}